package com.chuanyang.driverh.mainhome;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainhome.adapter.DriverEvaluateMotorcadeAdapter;
import com.chuanyang.driverh.mainhome.adapter.OftenPathMotorcadeAdapter;
import com.chuanyang.driverh.mainhome.adapter.RcyMotorcadeDriverAdapter;
import com.chuanyang.driverh.response2.join.EvaluateListBean;
import com.chuanyang.driverh.response2.join.FleetDataBean;
import com.chuanyang.driverh.response2.join.FleetDataResult;
import com.chuanyang.driverh.response2.join.FleetDriverMsgListBean;
import com.chuanyang.driverh.response2.join.FleetImgListBean;
import com.chuanyang.driverh.response2.join.FleetLineListBean;
import com.chuanyang.driverh.response2.join.ResultAddList;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.GlideCircleTransform;
import com.chuanyang.driverh.utils.widget.MyListView;
import com.chuanyang.driverh.utils.widget.SimpleRatingBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 相思无用，唯别而已。别期若有定，千般煎熬又何如，莫道黯然销魂，何处柳暗花明。
 * @author Fsl
 * 车队信息的界面
 * created at 2016/9/13 14:29
 */
public class MotorcadeMsgActivity extends BaseTitleActivity {

    private String motorcadeId;
    private ImageView iv_motorcade_details_img;
    private SimpleRatingBar rb_motorcade_details_star;
    private TextView tv_motorcade_details_name;
    private TextView tv_motorcade_dingyue;
    private TextView tv_motorcade_renzheng;
    private TextView tv_motorcade_details_car_id;
    private TextView tv_motorcade_details_phone;
    private TextView tv_motorcade_captain;
    private ImageView iv_motorcade_phone;

    private TextView tv_motorcade_img_more;//车队成员
    private RecyclerView rcv_motorcade_member;//车队成员

    private TextView tv_motorcade_total_ton;
    private TextView tv_motorcade_residue_ton;
    private TextView tv_motorcade_daily_ton_all;
    private TextView tv_motorcade_residue_daily_ton;

    private MyListView lv_motorcade_often_path;
    private TextView tv_motorcade_evaluate_more;
    private MyListView lv_motorcade_evaluate;

    private FleetDataBean fleetDataBean;
    private List<EvaluateListBean> evaluateListBean = new ArrayList<>();
    private List<FleetDriverMsgListBean> fleetDriverMsgListBean = new ArrayList<>();
    private List<FleetImgListBean> fleetImgListBean = new ArrayList<>();
    private List<FleetLineListBean> fleetLineListBean = new ArrayList<>();

    private List<FleetLineListBean> fleetLineListBeen = new ArrayList<>();
    private OftenPathMotorcadeAdapter oftenPathAdapter;

    private List<EvaluateListBean> evaluateListBeen = new ArrayList<>();
    private DriverEvaluateMotorcadeAdapter driverEvaluateAdapter;

    private RcyMotorcadeDriverAdapter rcyMotorcadeDriverAdapter;
    private LinearLayoutManager layoutManager;
    private int zaiyun = 0;

    private RelativeLayout rl_jiameng;

    @Override
    protected int getContentResId() {
        return R.layout.activity_motorcade_msg;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setBackImg();
        //setTitleText("车队信息");
        if(!TextUtils.isEmpty(getIntent().getStringExtra("data"))){
            motorcadeId = getIntent().getStringExtra("data");
        }
        iv_motorcade_details_img = (ImageView) findViewById(R.id.iv_motorcade_details_img);
        rb_motorcade_details_star = (SimpleRatingBar) findViewById(R.id.rb_motorcade_details_star);
        tv_motorcade_details_name = (TextView) findViewById(R.id.tv_motorcade_details_name);
        tv_motorcade_dingyue = (TextView) findViewById(R.id.tv_motorcade_dingyue);
        tv_motorcade_renzheng = (TextView) findViewById(R.id.tv_motorcade_renzheng);
        tv_motorcade_details_car_id = (TextView) findViewById(R.id.tv_motorcade_details_car_id);
        tv_motorcade_details_phone = (TextView) findViewById(R.id.tv_motorcade_details_phone);
        tv_motorcade_captain = (TextView) findViewById(R.id.tv_motorcade_captain);
        iv_motorcade_phone = (ImageView) findViewById(R.id.iv_motorcade_phone);
        iv_motorcade_phone.setOnClickListener(this);
        tv_motorcade_img_more = (TextView) findViewById(R.id.tv_motorcade_img_more);//车队成员
        rcv_motorcade_member = (RecyclerView) findViewById(R.id.rcv_motorcade_member);//车队成员
        tv_motorcade_total_ton = (TextView) findViewById(R.id.tv_motorcade_total_ton);
        tv_motorcade_residue_ton = (TextView) findViewById(R.id.tv_motorcade_residue_ton);
        tv_motorcade_daily_ton_all = (TextView) findViewById(R.id.tv_motorcade_daily_ton_all);
        tv_motorcade_residue_daily_ton = (TextView) findViewById(R.id.tv_motorcade_residue_daily_ton);
        lv_motorcade_often_path = (MyListView) findViewById(R.id.lv_motorcade_often_path);
        tv_motorcade_evaluate_more = (TextView) findViewById(R.id.tv_motorcade_evaluate_more);
        tv_motorcade_evaluate_more.setOnClickListener(this);
        lv_motorcade_evaluate = (MyListView) findViewById(R.id.lv_motorcade_evaluate);

        rl_jiameng = (RelativeLayout) findViewById(R.id.rl_jiameng);
        rl_jiameng.setOnClickListener(this);
        driverMsg(motorcadeId);
    }


    @Override
    protected void baseGoEnsure() {
        super.baseGoEnsure();
    }

    @Override
    public void initData() {

        oftenPathAdapter = new OftenPathMotorcadeAdapter(MotorcadeMsgActivity.this,fleetLineListBeen);
        lv_motorcade_often_path.setAdapter(oftenPathAdapter);
        driverEvaluateAdapter = new DriverEvaluateMotorcadeAdapter(MotorcadeMsgActivity.this, evaluateListBeen);
        lv_motorcade_evaluate.setAdapter(driverEvaluateAdapter);
        layoutManager = new LinearLayoutManager(MotorcadeMsgActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rcyMotorcadeDriverAdapter = new RcyMotorcadeDriverAdapter(fleetDriverMsgListBean, MotorcadeMsgActivity.this);
        rcv_motorcade_member.setLayoutManager(layoutManager);
        rcv_motorcade_member.setAdapter(rcyMotorcadeDriverAdapter);
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_motorcade_phone:
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + ""+fleetDataBean.getUserPhone());
                intent.setData(data);
                startActivity(intent);
                break;
            case R.id.tv_motorcade_evaluate_more:
//                Intent intent2 = new Intent(MotorcadeMsgActivity.this,EvaluateShowActivity.class);
//                startActivity(intent2);
                break;
            case R.id.rl_jiameng:
                addBusinessRelation(SharedPrefUtil.getUserId(),fleetDataBean.getFleetId()+"",
                        SharedPrefUtil.getUserId(),fleetDataBean.getFleetId()+"");
                break;
        }
    }
    private HashMap<String,String> mParams;
    //获取车队的信息
    private void driverMsg(String driverId){
        if (mParams == null) {
            mParams = new HashMap();
        }
        mParams.clear();
        mParams.put("fleetId", driverId);

        MyHttpUtil.selectFleetInfo(MotorcadeMsgActivity.this,mParams,
                new MyCallback<FleetDataResult>(MotorcadeMsgActivity.this,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(MotorcadeMsgActivity.this, "网络异常，请稍后再试");
                //springView.onFinishFreshAndLoad();
            }
            @Override
            public void onSuccess(FleetDataResult result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        fleetDataBean = result.getData();
                        evaluateListBean.addAll(fleetDataBean.getEvaluateList());
                        fleetDriverMsgListBean.addAll(fleetDataBean.getFleetDriverMsgList());
                        fleetImgListBean.addAll(fleetDataBean.getFleetImgList());
                        fleetLineListBean.addAll(fleetDataBean.getFleetLineList());
                        setMsg();
                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(MotorcadeMsgActivity.this,result.getMsg());
                }
            }
        });
    }

    private void setMsg(){

        Glide.with(MotorcadeMsgActivity.this).load("").crossFade().placeholder(R.mipmap.group_chat).
                error(R.mipmap.group_chat).transform(new GlideCircleTransform(MotorcadeMsgActivity.this)).into(iv_motorcade_details_img);

        rb_motorcade_details_star.setRating(Float.parseFloat(fleetDataBean.getStar()+""));
        tv_motorcade_details_name.setText(fleetDataBean.getFleetName());
        tv_motorcade_captain.setText("队长："+fleetDataBean.getFleetName());
        tv_motorcade_details_phone.setText(fleetDataBean.getUserPhone());
        for(int i = 0 ; i < fleetDataBean.getFleetDriverMsgList().size() ; i++){
            if(fleetDataBean.getFleetDriverMsgList().get(i).getTransportState() != null){
                if(fleetDataBean.getFleetDriverMsgList().get(i).getTransportState() == "1"){
                    zaiyun = zaiyun +1;
                }
            }
        }
        tv_motorcade_img_more.setText("在运"+zaiyun+"辆");
        //tv_belongs_company.setText(fleetDataBean);
        tv_motorcade_total_ton.setText("共计"+fleetDataBean.getCapacity()+"吨");

        tv_motorcade_daily_ton_all.setText("共计"+fleetDataBean.getFleetDriverMsgList().size()+"辆");
        tv_motorcade_residue_daily_ton.setText("空闲"+(fleetDataBean.getFleetDriverMsgList().size()-zaiyun)+"辆");

        oftenPathAdapter.notifyDataSetChanged();
        driverEvaluateAdapter.notifyDataSetChanged();
    }
    //加盟接口
    private void addBusinessRelation(String initiator,String pendi,String driverId,String fleetId){

        DialogUtil.showLoadingDialog(MotorcadeMsgActivity.this,"加载中...");
        Map<String,Object> paramssss =new HashMap<String, Object>();
        paramssss.put("initiator", initiator);
        paramssss.put("pendi", pendi);
        paramssss.put("driverId", driverId);
        paramssss.put("fleetId", fleetId);

        MyHttpUtil.addBusinessRelation(MotorcadeMsgActivity.this,paramssss, new MyCallback<Result>(
                MotorcadeMsgActivity.this, new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                Toast.makeText(MotorcadeMsgActivity.this,"加盟失败",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        Toast.makeText(MotorcadeMsgActivity.this,result.getMsg(),Toast.LENGTH_SHORT).show();
                        LogUtils.d(result.getCode()+result.getMsg());
                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(MotorcadeMsgActivity.this,result.getMsg());
                }

            }
        });
    }
}
